/**
 * @author Clark
 * @email haixuanwoTxh@gmail.com
 * @date 2021-10-20
 */

#include <gtest/gtest.h>
#include "talk_protocol.h"
#include <stdio.h>

#define OPEN_ALL 1
#define CHECK_SUM 0
#define REPLACE_SPACE_STR 0
#define BUILD_PACKET 0
#define PARSE_PACKET 0

#if (CHECK_SUM || OPEN_ALL)
TEST(checksum, 01)
{
	unsigned short checksum = 1;
	TalkProtocol tp;
	unsigned char data[]={0x1, 0x2, 0x3, 0x4, 0x5};

	// 1、参数合法性测试

	EXPECT_FALSE(tp.get_checksum(nullptr, 1, checksum));
	printf("checksum[%u]\n", checksum);

	EXPECT_FALSE(tp.get_checksum(data, -1, checksum));
	printf("checksum[%u]\n", checksum);

	EXPECT_TRUE(tp.get_checksum(data, 0, checksum));
	printf("checksum[%u]\n", checksum);

	EXPECT_TRUE(tp.get_checksum(data, 1, checksum));
	printf("checksum[%u]\n", checksum);

	// 2、函数功能结果测试
	EXPECT_TRUE(tp.get_checksum(data, sizeof(data), checksum));
	printf("checksum[%u]\n", checksum);

}
#endif

#if (BUILD_PACKET || OPEN_ALL)

TEST(build_packet, 0)
{
	TalkProtocol tp;
	Action action = MESSAGE;

	unsigned char data[] = {0x1, 0x2, 0x3, 0x4, 0x5};
	unsigned char packet[2048] = {0};
	int packetLen = 0;
//bool TalkProtocol::build_protocol_packet(Action action, char* data, int dataLen, char* packet, int packetBufSize, int &packetLen)

	// 1、参数合法性
	EXPECT_FALSE(tp.build_protocol_packet(action, nullptr, 1, packet, sizeof(packet), packetLen));
	EXPECT_FALSE(tp.build_protocol_packet(action, data, -1, packet, sizeof(packet), packetLen));

	EXPECT_FALSE(tp.build_protocol_packet(action, data, sizeof(data), nullptr, sizeof(packet), packetLen));
	EXPECT_FALSE(tp.build_protocol_packet(action, data, sizeof(data), packet, 9, packetLen));

	EXPECT_TRUE(tp.build_protocol_packet(action, data, 0, packet, sizeof(packet), packetLen));
	tp.show_hex(packet, packetLen);

	EXPECT_TRUE(tp.build_protocol_packet(action, data, 1, packet, sizeof(packet), packetLen));
	tp.show_hex(packet, packetLen);

	EXPECT_TRUE(tp.build_protocol_packet(action, data, sizeof(data), packet, sizeof(packet), packetLen));
	tp.show_hex(packet, packetLen);
}
#endif

#if (PARSE_PACKET || OPEN_ALL)
TEST(parse_packet, 01)
{
	TalkProtocol tp;
	Action action = MESSAGE;

	unsigned char data[1024] = {0};

	//unsigned char packet[] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xee, 0xee};
	unsigned char packet[] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x01, 0x01, 0x02, 0xf0, 0xee, 0xee};



	unsigned char data1[] = {0x1, 0x2, 0x3, 0x4, 0x5};
	unsigned char packet1[2048] = {0};
	int packetLen1 = 0;
	EXPECT_TRUE(tp.build_protocol_packet(action, data1, sizeof(data1), packet1, sizeof(packet1), packetLen1));
	tp.show_hex(packet1, packetLen1);

	int dataLen = 0;

	EXPECT_FALSE(tp.parse_protocol_packet(nullptr, sizeof(packet),action,data,sizeof(data), dataLen));
	EXPECT_FALSE(tp.parse_protocol_packet(packet, 9,action,data,sizeof(data), dataLen));
	EXPECT_FALSE(tp.parse_protocol_packet(packet, sizeof(packet),action,nullptr,sizeof(data), dataLen));
	EXPECT_FALSE(tp.parse_protocol_packet(packet, sizeof(packet),action,data, 0, dataLen));

	EXPECT_TRUE(tp.parse_protocol_packet(packet1, packetLen1,action,data,sizeof(data), dataLen));
	printf("action[%d]\n", action);
	tp.show_hex(data, dataLen);

	unsigned char packet2[] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x16, 0xee, 0xee};
	EXPECT_TRUE(tp.parse_protocol_packet(packet2, sizeof(packet2),action,data,sizeof(data), dataLen));
	printf("action[%d]\n", action);
	tp.show_hex(data, dataLen);

	unsigned char packet3[] = {0xf3, 0xf3, 0x00, 0x02, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x16, 0xee, 0xee};
	EXPECT_FALSE(tp.parse_protocol_packet(packet3, sizeof(packet3),action,data,sizeof(data), dataLen));

	unsigned char packet4[] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x16, 0xee, 0xee};
	EXPECT_FALSE(tp.parse_protocol_packet(packet4, sizeof(packet4),action,data,sizeof(data), dataLen));

	unsigned char packet5[] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x17, 0xee, 0xee};
	EXPECT_FALSE(tp.parse_protocol_packet(packet5, sizeof(packet5),action,data,sizeof(data), dataLen));

	unsigned char packet6[] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x16, 0xe1, 0xee};
	EXPECT_FALSE(tp.parse_protocol_packet(packet6, sizeof(packet6),action,data,sizeof(data), dataLen));
}
#endif


#if (OPEN_ALL || REPLACE_SPACE_STR)
bool replace_space_in_string(std::string s)
{
	#define SPACE ' '
	#define REPLACE_STR "0x20"
	int pos = 0;

	for(int i = 0; i < s.length(); i++)
	{
		if (SPACE == s.at(i))
		{
			s.replace(i, 1, REPLACE_STR);
		}
	}

	std::cout<<s<<std::endl;
	return true;
}

TEST(REPLACE_SPACE, 01)
{
	std::string s = "fd sdf ff ff ss";
	EXPECT_TRUE(replace_space_in_string(s));

	std::string s1 = " ";
	EXPECT_TRUE(replace_space_in_string(s1));

	std::string s2 = "";
	EXPECT_TRUE(replace_space_in_string(s2));

	std::string s3 = "     ";
	EXPECT_TRUE(replace_space_in_string(s3));

}
#endif

int main(int argc, char *argv[])
{
	testing::InitGoogleTest(&argc,argv);
	return RUN_ALL_TESTS();
}
